<?php
require_once 'common.php';

$minLevel 	= MooForm::request('minLevel');
$maxLevel 	= MooForm::request('maxLevel');
$server 	= MooForm::request('server');
$registerDate 	= MooForm::request('registerDate');

// 注册日期
$registerTime   =   strtotime($registerDate);
$registerDateFormat = date('Y-m-d H:i:s', $registerTime);
$sencordDateFormat  = date('Y-m-d H:i:s', strtotime('+1 day',$registerTime));

if(empty($minLevel) || empty($maxLevel) || empty($server)) {
	$returnData['totalNum'] = -1;		// 异常
	exit(MooJson::encode($returnData));
}

$fetchServerUrl = MooConfig::get('main.url.serverlist');
$serverUrl  = $fetchServerUrl . "?game=" . "qmby";
	
$serverInfo = MooUtil::curl_send($serverUrl);
$serverInfoArr  =  MooJson::decode($serverInfo, true);
MooArray::tdSort($serverInfoArr, 'platform', SORT_ASC, 'serverId', SORT_ASC);

$dbConfs = array();
foreach($serverInfoArr as $key => $serverInfo) {
	$k = $serverInfo['platform'] . "-" . $serverInfo['serverId'];
	$dbUrl 	= $serverInfo['dbUrl'];
	$dbArr = explode("//", $dbUrl);
	$dbArrs = explode("/", $dbArr[1]);
	$dbHost = $dbArrs[0];  // db host
	$dbName = $dbArrs[1];  // db name
	$dbUser = $serverInfo['dbUser'];// user
	$dbPwd 	= $serverInfo['dbPwd']; // pwd
	$conf = array(
		'host' => $dbHost,	
	 	'user' => $dbUser,	
	 	'pwd' => $dbPwd,	
	 	'dbName' => $dbName,	
	 	'char' => 'utf8',	
	);
	$dbConfs[$k] = $conf;
}

$excel = ROOT_PATH . "/www/loginIds_all.xls";
$data = MooObj::get('Control_ExcelConfMaker_Excel')->read($excel, 0);

/* $dbConfs = array(
	'android-1' => array(
	 	'host' => '10.10.134.8:3306',	
	 	'user' => 'root',	
	 	'pwd' => '@fox0426^feihu^db',	
	 	'dbName' => 'qmby',	
	 	'char' => 'utf8',	
	),
	'android-2' => array(
	 	'host' => '10.10.128.139:3306',	
	 	'user' => 'root',	
	 	'pwd' => '@fox0426^feihu^db',	
	 	'dbName' => 'qmby',	
	 	'char' => 'utf8',	
	),
	'android-3' => array(
	 	'host' => '10.10.113.17:3306',	
	 	'user' => 'root',	
	 	'pwd' => '@fox0426^feihu^db',	
	 	'dbName' => 'qmby',	
	 	'char' => 'utf8',	
	),
); */

/* $conf = array();
$conf['host'] 	= 'localhost:3306';
$conf['user'] 	= 'root';
$conf['pwd']  	= '';
$conf['dbName'] = 'qmby_dev';
$conf['char'] 	= 'utf8'; */ 

$conf = $dbConfs[$server];

$link = mysql_connect($conf['host'], $conf['user'], $conf['pwd']);
if (!$link) {
	echo '数据库连接失败!' . mysql_error();
	exit;
}

if (!@mysql_select_db($conf['dbName'])) {
	echo '选择数据库失败' . mysql_error();
	exit;
}
mysql_set_charset($conf['char']);

$sql = "SELECT count(*) as totalNum FROM player WHERE level >=".$minLevel." and level <=".$maxLevel . " and createTime >='" . $registerDateFormat . "' and createTime <'" . $sencordDateFormat ."' and loginTime <'".$sencordDateFormat."'";


$result = mysql_query($sql, $link);

$totalNum  = 0;
while($res = mysql_fetch_array($result)) {
	$totalNum = $res['totalNum'];
}

$returnData['totalNum'] = $totalNum;
exit(MooJson::encode($returnData));
